package com.djl.entity;

import com.djl.exception.BootException;
import com.djl.supports.PluginInjector;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class EntitySqlBuilder {

    private ConcurrentHashMap<String, Map<String,Object>> propertiesMap = PluginInjector.getPropertiesMap();

    private StringBuffer condition = new  StringBuffer(" where ");

    private static final String AND = " and ";

    private static final String IS = "=";

    private static final String IN = "in";

    private int num;

    public void createSqlBuilder(Criteria criteria) {
        Map<String, Object> queryParamMap = criteria.paramMap;
        Map<String, Object> paramsMap = propertiesMap.get(criteria.table);
        if(!queryParamMap.isEmpty()){
            Set<String> keys = queryParamMap.keySet();
            keys.forEach(s -> {
                if(queryParamMap.size()>1){
                    num++;
                    if(num!=queryParamMap.size())
                    condition.append(paramsMap.get(s)).append(IS).append(queryParamMap.get(s)).append(AND);
                    else condition.append(paramsMap.get(s)).append(IS).append(queryParamMap.get(s));
                }else{
                    condition.append(paramsMap.get(s)).append(IS).append(queryParamMap.get(s));
                }
            });
        }else throw new BootException("Query参数错误,不应该存在的异常!");

    }
}
